package leetcode.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangmaolin1
 * @date 2022/1/3 下午6:28
 */
public class _530二叉搜索树的最小绝对差 {

    int min = Integer.MAX_VALUE;
    int pre=-1;

    public int getMinimumDifference(TreeNode root) {
        if (root == null) {
            return 0;
        }
        midOrder(root);

        return min;
    }

    private void midOrder(TreeNode root) {
        if (root==null){
            return;
        }

        if (root.left != null) {
            midOrder(root.left);
        }
        if (pre==-1){
            pre=root.val;
        }else {
            min=Math.min(min,root.val-pre);
            pre=root.val;
        }
        if (root.right != null) {
            midOrder(root.right);
        }
    }

    /**
     *    236
     *  106   701
     *    227     911
     * @param args
     */
    public static void main(String[] args) {
        TreeNode root = new TreeNode(236);
        root.left = new TreeNode(106);
        root.right = new TreeNode(701);
        root.left.right = new TreeNode(227);
        root.right.right = new TreeNode(911);
        System.out.println(new _530二叉搜索树的最小绝对差().getMinimumDifference(root));
    }
}
